package cn.stronglink.oil.mq;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

import cn.stronglink.oil.entity.DeviceAlarmInfo;
import cn.stronglink.oil.entity.DeviceRealTimeInfo;
import cn.stronglink.oil.user.UserInfo;

/**
 * Created by Tgb on 2017/10/28.
 * 设备气体数据管理类
 */

public class DeviceGasDataManager implements IMqListener {
    private MqMessageReceiverClient mqClient;
    private IDeviceGasDataListener deviceGasDataListener;

    public DeviceGasDataManager() {
        mqClient = new MqMessageReceiverClient(UserInfo.getInstance().getMqRoutingKey());
        mqClient.setMqListener(this);
        mqClient.startConnect();
    }

    private static DeviceGasDataManager instance;

    public static DeviceGasDataManager getInstance() {
        if (instance == null) {
            instance = new DeviceGasDataManager();
        }
        return instance;
    }

    public void setDeviceGasDataListener(IDeviceGasDataListener deviceGasDataListener) {
        this.deviceGasDataListener = deviceGasDataListener;
    }

    public List<DeviceRealTimeInfo> getDeviceGasInfos() {
        return (mqClient == null || mqClient.getDeviceGasInfos().size() == 0) ? null : mqClient.getDeviceGasInfos();
    }

    public List<DeviceRealTimeInfo> queryDeviceGasInfos(Integer gasType, Integer deviceId) {
//        if (gasType == null && deviceId == null) {
//            return (mqClient == null || mqClient.getDeviceGasInfos().size() == 0) ? null : mqClient.getDeviceGasInfos();
//        }

        if (gasType != null) {
            if (deviceId == null) {
                return this.queryDeviceGasInfosByGasType(mqClient.getDeviceGasInfos(), gasType);
            } else {
                return this.queryDeviceGasInfosByGasType(this.queryDeviceGasInfosByDeviceId(deviceId), gasType);
            }
        } else {
            if (deviceId == null) {
                return (mqClient == null || mqClient.getDeviceGasInfos().size() == 0) ? null : mqClient.getDeviceGasInfos();
            } else {
                return this.queryDeviceGasInfosByDeviceId(deviceId);
            }
        }
    }

    /**
     * 通过气体类型查询信息
     *
     * @param gasType
     * @return
     */
    private List<DeviceRealTimeInfo> queryDeviceGasInfosByGasType(List<DeviceRealTimeInfo> list, int gasType) {
        List<DeviceRealTimeInfo> realTimeInfoList = null;
        List<DeviceRealTimeInfo.GasDevicesBean> gasDevicesList = null;
        List<DeviceRealTimeInfo.GasDevicesBean.GasInfosBean> gasInfoList = null;
        if (list != null && list.size() > 0) {
            realTimeInfoList = new ArrayList<>();
            for (int i = 0; i < list.size(); i++) {
                gasDevicesList = new ArrayList<>();
                DeviceRealTimeInfo deviceRealTimeInfo = list.get(i);
                for (int j = 0; j < deviceRealTimeInfo.getGasDevices().size(); j++) {
                    gasInfoList = new ArrayList<>();
                    DeviceRealTimeInfo.GasDevicesBean gasDevicesBean = deviceRealTimeInfo.getGasDevices().get(j);
                    for (int k = 0; k < gasDevicesBean.getGasInfos().size(); k++) {
                        DeviceRealTimeInfo.GasDevicesBean.GasInfosBean gasInfosBean = gasDevicesBean.getGasInfos().get(k);
                        if (gasInfosBean.getGasType() == gasType) {
                            gasInfoList.add(gasInfosBean);
                            continue;
                        }
                    }
                    if (gasInfoList.size()==0)continue;
                    DeviceRealTimeInfo.GasDevicesBean gasDevice = new DeviceRealTimeInfo.GasDevicesBean();
                    gasDevice.setDeviceId(gasDevicesBean.getDeviceId());
                    gasDevice.setDeviceLat(gasDevicesBean.getDeviceLat());
                    gasDevice.setDeviceLng(gasDevicesBean.getDeviceLng());
                    gasDevice.setGasInfos(gasInfoList);
                    gasDevice.setGasTime(gasDevicesBean.getGasTime());
                    gasDevicesList.add(gasDevice);
                }
                if (gasDevicesList.size()==0)continue;
                DeviceRealTimeInfo realTimeInfo = new DeviceRealTimeInfo();
                realTimeInfo.setDeviceSerial(deviceRealTimeInfo.getDeviceSerial());
                realTimeInfo.setWeatherDevice(deviceRealTimeInfo.getWeatherDevice());
                realTimeInfo.setGasDevices(gasDevicesList);
                realTimeInfoList.add(realTimeInfo);
            }
        }
        return realTimeInfoList;
    }

    private List<DeviceRealTimeInfo> queryDeviceGasInfosByDeviceId(int deviceId) {
        List<DeviceRealTimeInfo> realTimeInfoList = null;
        List<DeviceRealTimeInfo.GasDevicesBean> gasDevicesList = null;
        if (mqClient != null && mqClient.getDeviceGasInfos().size() > 0) {
            realTimeInfoList = new ArrayList<>();
            for (int i = 0; i < mqClient.getDeviceGasInfos().size(); i++) {
                gasDevicesList = new ArrayList<>();
                DeviceRealTimeInfo realTimeInfo = mqClient.getDeviceGasInfos().get(i);
                for (int j = 0; j < realTimeInfo.getGasDevices().size(); j++) {
                    DeviceRealTimeInfo.GasDevicesBean gasDevicesBean = realTimeInfo.getGasDevices().get(j);
                    if (gasDevicesBean.getDeviceId() == deviceId) {
                        gasDevicesList.add(gasDevicesBean);
                    }
                }
                if (gasDevicesList.size()==0)continue;
                DeviceRealTimeInfo deviceRealTimeInfo = new DeviceRealTimeInfo();
                deviceRealTimeInfo.setGasDevices(gasDevicesList);
                deviceRealTimeInfo.setDeviceSerial(realTimeInfo.getDeviceSerial());
                deviceRealTimeInfo.setWeatherDevice(realTimeInfo.getWeatherDevice());
                realTimeInfoList.add(deviceRealTimeInfo);
            }
        }
        return realTimeInfoList;
    }

    @Override
    public void onConnect() {
        if (this.deviceGasDataListener != null) {
            this.deviceGasDataListener.onMqConnect();
        }
    }

    @Override
    public void onDisConnect() {
        if (this.deviceGasDataListener != null) {
            this.deviceGasDataListener.onMqDisConnect();
        }
    }

    @Override
    public void onRecData(DeviceRealTimeInfo realTimeInfo) {
        if (this.deviceGasDataListener != null) {
            this.deviceGasDataListener.onRecRealTimeData(realTimeInfo);
        }
    }

    @Override
    public void onRecAlarmData(DeviceAlarmInfo deviceAlarmInfo) {
        if (this.deviceGasDataListener != null) {
            this.deviceGasDataListener.onRecAlarmData(deviceAlarmInfo);
        }
    }

    public void close() throws IOException, TimeoutException {
        if (this.mqClient!=null){
            this.mqClient.close();
        }
    }
}
